package com.example.hotelmanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.hotelmanager.entity.Cost;
import com.example.hotelmanager.entity.CostType;
import com.example.hotelmanager.entity.vo.Response;
import com.example.hotelmanager.entity.vo.ResponseMsg;
import com.example.hotelmanager.service.*;
import com.example.hotelmanager.utils.JwtUtill;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :chenjie
 * @date :Created 2022/11/28 22:24
 */
@CrossOrigin
@RestController
@Slf4j
public class CostTypeController {
    @Autowired
    private CostService costService;
    @Autowired
    private JwtUtill jwtUtill;
    @Autowired
    private BackgroundService backgroundService;
    @Autowired
    private FrontService frontService;
    @Autowired
    private CostTypeService costTypeService;


    @GetMapping("/getAllCostType")
    public Response getAllCostType(HttpServletRequest request){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){
            List<CostType> costTypes = costTypeService.getAllCostType();
            if(costTypes != null && costTypes.size()>0){
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("costTypes",costTypes);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    @PostMapping("/addCostType")
    public Response addCostType(HttpServletRequest request, CostType costType){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null){
            costTypeService.saveOrUpdate(costType);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    @DeleteMapping("/deleteCostType")
    public Response deleteCostType(HttpServletRequest request,Integer id){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null){
            LambdaQueryWrapper<Cost> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(Cost::getCostTypeId,id);
            costService.remove(queryWrapper);
            costTypeService.removeById(id);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    /**
     * 条件查询
     * @param request
     * @param id
     * @return
     */
    @GetMapping("/getCostTypeById")
    public Response getCostTypeById(HttpServletRequest request,Integer id){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){
            CostType costType = costTypeService.getById(id);
            if(costType != null ){
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("costType",costType);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }


    @GetMapping("/getCostTypeByName")
    public Response getCostTypeByName(HttpServletRequest request,String name){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){
            LambdaQueryWrapper<CostType> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(CostType::getName,name);
            List<CostType> costTypes = costTypeService.list(queryWrapper);
            if(costTypes != null ){
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("costTypes",costTypes);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
}
